package com.example.a10928.weifriend.ui;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a10928.weifriend.R;
import com.noober.background.BackgroundLibrary;

public abstract class BaseActivity extends AppCompatActivity {

    protected enum ThemeEnum {
        Default, Back_Default, Menu_No_Back;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        BackgroundLibrary.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        initData();
        initView();
    }

    public abstract void initData();

    public abstract void initView();

    public abstract int getContentView();


    /**
     * @param themeEnum
     * @param data      参数一(标题) 参数二(标题栏的菜单名字)
     */
    protected View setTitleText(@NonNull ThemeEnum themeEnum, @NonNull String... data) {
        View mActionBarView = null;
        if (data == null) data = new String[]{""};
        for (int i = 0; i < data.length; i++) {
            if (data[i] == null) {
                data[i] = "";
            }
        }
        switch (themeEnum) {
            default:
            case Default:
                mActionBarView = setTitle(ThemeEnum.Default, data[0]);
                break;
            case Back_Default:
                mActionBarView = setTitle(ThemeEnum.Back_Default, data[0]);
                break;
            case Menu_No_Back:
                mActionBarView = setTitle(ThemeEnum.Menu_No_Back, data[0], data[1]);
                break;
        }

        if (mActionBarView == null) return null;
        ((ImageView) mActionBarView.findViewById(R.id.theme_back_iv)).setOnClickListener(v -> {
            finish();
        });
        getSupportActionBar().show();
        return mActionBarView;
    }

    @SuppressLint("RestrictedApi")
    private View setTitle(ThemeEnum themeEnum, String... data) {
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_title_back_default, null);
        if (mActionBarView.getVisibility() == View.GONE)
            mActionBarView.setVisibility(View.VISIBLE);
        TextView tv = mActionBarView.findViewById(R.id.actionbar_title);
        TextView tv_menu = mActionBarView.findViewById(R.id.actionbar_title_menu);
        ImageView iv = mActionBarView.findViewById(R.id.theme_back_iv);
        switch (themeEnum) {
            default:
            case Default:
                tv.setText(data[0]);
                break;
            case Back_Default:
                tv.setText(data[0]);
                iv.setVisibility(View.VISIBLE);
                break;
            case Menu_No_Back:
                tv.setText(data[0]);
                tv_menu.setText(data[1]);
                tv_menu.setVisibility(View.VISIBLE);
                break;
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            return null;
        }
        actionBar.setCustomView(mActionBarView, lp);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setElevation(0);//去掉阴影
        //修改actionbar背景颜色
        ColorDrawable drawable = new ColorDrawable(Color.WHITE);
        actionBar.setBackgroundDrawable(drawable);
        actionBar.setShowHideAnimationEnabled(false);
        return mActionBarView;
    }

}
